using System;
using System.Data.SqlClient;

namespace Coleotrack.Common.Registration
{
    public class DatabaseConnector : IDatabaseConnector
    {
        public bool ConnectionIsValid(string hostName, bool integratedAuth, string username, string password, string database)
        {
            try
            {
                var sqlConnectionStringBuilder = new SqlConnectionStringBuilder();
                sqlConnectionStringBuilder.DataSource = hostName;
                if (integratedAuth)
                {
                    sqlConnectionStringBuilder.IntegratedSecurity = false;
                    sqlConnectionStringBuilder.UserID = username;
                    sqlConnectionStringBuilder.Password = password;
                }
                else
                {
                    sqlConnectionStringBuilder.IntegratedSecurity = true;
                }

                sqlConnectionStringBuilder.InitialCatalog = database;

                using (var sqlConnection = new SqlConnection(sqlConnectionStringBuilder.ToString()))
                {
                    sqlConnection.Open();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}